// serialtoolwidget.h
#ifndef SERIALTOOLWIDGET_H
#define SERIALTOOLWIDGET_H
#pragma once

#include "receive_processor.h"
#include "historypage.h"
#include "devicemonitor.h"
#include "modbuspoller.h"
#include <QWidget>
#include <QSerialPort>
#include <QTimer>
#include <QComboBox>
#include <QPushButton>
#include <QPlainTextEdit>
#include <QCheckBox>
#include <QLabel>
#include <QGroupBox>
#include <QLineEdit>
#include <QTextEdit>
#include <QSpinBox>
#include <QDateTime>
#include <QListWidget>
#include <QFile>
#include <QListWidget>
#include <QListWidgetItem>
#include <QModbusRtuSerialMaster>
#include <QVariant>
class SerialToolWidget : public QWidget
{
    Q_OBJECT

public:
    explicit SerialToolWidget(DeviceMonitor *device_monitor_input,HistoryPage*, QWidget *parent = nullptr);
    ~SerialToolWidget();

    // QString receive_data_save_location;//接收的数据直接保存到数据库中
    //消息日志文件保存的位置
    QString debug_data_save_location;
    QString receive_data_save_location;
    QString receive_config_location;
    QString send_config_location;
    //实时数据类的变量，用于实时更新数据显示
    DeviceMonitor *device_monitor;
    //历史数据显示页，用于将数据加入到本地的数据库
    HistoryPage *history_page;
    ///数据处理器的集合
    QList<Receive_Processor*> *receive_processor_list;
    ModbusPoller *poller;
private slots:
    //系统设置相关槽
    void Ensure_Path_Exits(QString path,QString&var);
    //串口相关槽
    void onPortOpenClicked();
    void initPortConfig();

    //数据接收相关槽
    void onClearRxClicked();
    void updateStatistics();
    void Save_Receive_Zone();
    void handleError(QSerialPort::SerialPortError error);

    //数据发送相关槽
    void onAutoReadClicked();
    //日志相关槽
    void Clear_Debug_Zone();
    void Save_Debug_Zone();
    void auto_simulate_data(int checked_state);
private:

    void initUI();
    void initConnections();//初始化信号连接
    void re_connect();//尝试重新连接，并发送数据
    void setUiEnabled(bool state);
    void showStatusMessage(const QString &message);
    Receive_Processor* Receive_Config(QString &file_name);
    void Debug_Log(QString s);
    void Update_Receive_Config();
    void Update_Send_Config();
    qint16 twosComplementConvert(quint16 value);

    QList<double> *param_Range;
    QTimer * data_simulate_timer;
    // QModbusRtuSerialMaster *modbusMaster;

    // UI Components
    ///端口
    QComboBox *cbPort;
    ///波特率
    QComboBox *cbBaud;

    QComboBox *cbData;
    ///
    QComboBox *cbParity;
    ///停止位
    QComboBox *cbStop;
    ///自动打开上次连接的串口
    QCheckBox *auto_reopen_port;
    ///打开串口按钮
    QPushButton *btnOpenPort;
    ///刷新端口按钮
    QPushButton *btn_flushPort;
    QGroupBox *receive_config_groupbox;
    QListWidget *receive_config_listwidget;
    //接收区
    QPlainTextEdit *txtReceive;
    ///以十六进制显示
    QCheckBox *chkHexDisplay;
    QCheckBox *chkAutoReload;
    ///输出到日志窗口
    QCheckBox *receive_Log;
    ///保存到数据库
    QCheckBox *receive_save2db;
    ///开启数据模拟
    QCheckBox *start_data_simulate;
    ///保存接收区到软件目录下的Receive文件夹中
    QPushButton *btn_Receive_save;
    // QCheckBox *chk_Cycle_read;//当前设置只能以周期方式从串口读入数据
    // QTimer * timer_receive;
    // bool receivable;
    // QCheckBox *chkTimestamp;
    ///清空发送区按钮
    QPushButton *btnClearRx;


    // 数据发送
    //1
    QLabel * send1Label;
    QCheckBox *send_one_time;
    QLabel*send_Cycle_Label;//发送周期文本
    QCheckBox*send_apply_Cycle;//应用发送周期到所有发送区
    QSpinBox *sb_cycle_edit;//重新发送的周期
    QPushButton *btn_Auto_ReSend;
    QCheckBox *auto_resend_config_file;//自动重发配置文件中的内容？
    QTimer * auto_resend_config_file_timer;
    QLabel *lblStats;
    // 添加状态栏标签
    QLabel *statusLabel;
    QSerialPort *serialPort;
    QTimer *statTimer;
    qint64 bytesReceived = 0;
    qint64 bytesSent = 0;
    bool port_opened=false;
    //用于Debug的区域
    QTextEdit *Debug_Zone;
    QGroupBox *send_config_groupbox;
    QListWidget *send_config_listwidget;//发送去配置文件的框体
    QList<QListWidgetItem*> *send_configitem_list;//发送区各发送配置文件的列表

    //用于还原和保存之前选中项
    QMap<QString,QString> settings_pair;

};

#endif // SERIALTOOLWIDGET_H
